//
//  MPCActionSheetC1Cell.m
//  MPCActionSheet
//
//  Created by macRong on 2016/7/15.
//  Copyright (c) 2017 jxzjxz. All rights reserved.
//

#import "MPCActionSheetC1Cell.h"
#import "MPCActionSheetDefines.h"

@interface MPCActionSheetC1Cell ()

/**
 *  Highlighted View.
 */
@property (nonatomic, weak) UIView *highlightedView;

@end

@implementation MPCActionSheetC1Cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        
        // 初始化界面
        [self initViews];
    }
    
    return self;
}

- (void)initViews
{
    UIView *highlightedView  = [[UIView alloc] init];
    highlightedView.backgroundColor = self.cellSeparatorColor;
    highlightedView.clipsToBounds   = YES;
    highlightedView.hidden          = YES;
    [self.contentView addSubview:highlightedView];
    self.highlightedView = highlightedView;
    [highlightedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.adjustsFontSizeToFitWidth = YES;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 10.0f, 0, 10.0f));
    }];
    
    UIView *lineView  = [[UIView alloc] init];
    lineView.backgroundColor = self.cellSeparatorColor;
    lineView.contentMode   = UIViewContentModeBottom;
    lineView.clipsToBounds = YES;
    [self.contentView addSubview:lineView];
    self.lineView = lineView;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.equalTo(@0.5f);
    }];
}

- (void)setCellSeparatorColor:(UIColor *)cellSeparatorColor
{
    _cellSeparatorColor = cellSeparatorColor;
    
    self.highlightedView.backgroundColor = cellSeparatorColor;
    self.lineView.backgroundColor        = cellSeparatorColor;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (self.tag == MPC_ACTION_SHEET_CELL_HIDDE_LINE_TAG)
    {
        self.lineView.hidden = YES;
    }
    else
    {
        self.lineView.hidden = highlighted;
    }
    
    self.highlightedView.hidden = !highlighted;
    
    
    // 获取 contentView 所有子控件
    NSArray<__kindof UIView *> *subViews = self.contentView.subviews;
    
    for (UIView *v in subViews)
    {
        if ([v isKindOfClass:[UILabel class]])
        {
            UILabel *lab = (UILabel *)v;
            UIColor *col = lab.textColor;
            //                self.currentColor = col;
            lab.textColor = [col colorWithAlphaComponent:highlighted ? 0.5f : 1.0f];
            break;
        }
    }
    
    [super setHighlighted:highlighted animated:animated];
}


@end
